/**
 * Copyright (c) 2011 Nokia Corporation.
 */

import QtQuick 1.1
import com.nokia.meego 1.0
import "HarmattanConstants.js" as Constants

Page {
    Label {
        id: title
        height: Constants.DEFAULT_TITLE_LABEL_HEIGHT

        anchors {
            top: parent.top
            left: parent.left
            right: parent.right
        }

        color: "white"
        z: 1
        verticalAlignment: Text.AlignVCenter
        font.pixelSize: height / 2;
        text: sensorManager.sensorType
        elide: Text.ElideRight
    }

    // Simple border
    Rectangle {
        id: titleBorder
        width: parent.width
        height: 2
        anchors.top: title.bottom
        z: 1
        color: "#303030"
    }

    Flickable {
        id: sensorContentItem

        anchors {
            top: titleBorder.bottom
            left: parent.left
            right: parent.right
            bottom: parent.bottom
        }

        contentHeight: dataRateText.height + dataRateSlider.height
                       + sensorListView.height;

        Text {
            id: dataRateText
            height: 40

            anchors {
                top: parent.top
                left: parent.left
                margins: Constants.DEFAULT_MARGIN
            }

            color: "white"
            font.pixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
            verticalAlignment: Text.AlignVCenter
            text: "Sensor data rate: " + dataRateSlider.value
        }

        Slider {
            id: dataRateSlider

            property bool valueSetByManager: false

            /**
             * Sets the value of the slider. This functions should only be
             * called if the value is set by the manager since the valueChanged
             * signal is not emitted after calling this function.
             *
             * @param newValue The value to set.
             */
            function setValue(newValue)
            {
                valueSetByManager = true;
                value = newValue;
            }

            width: parent.width
            height: 100
            anchors.top: dataRateText.bottom
            stepSize: 1
            valueIndicatorVisible: true
            minimumValue: 0
            maximumValue: 100

            onValueChanged: {
                if (valueSetByManager) {
                    valueSetByManager = false;
                }
                else {
                    // Set the new data rate according to the slider value.
                    sensorManager.setSensorDataRate(dataRateSlider.value);
                }
            }
        }

        Connections {
            target: sensorManager

            onSensorDataRateChanged: {
                console.debug("Data rate changed to " + dataRate);
                dataRateSlider.setValue(dataRate);
            }

            onSensorTypeChanged: {
                console.debug("Sensor type changed. Reset the data rate slider.");
                dataRateSlider.setValue(sensorManager.sensorDataRate);
            }
        }

        ListView {
            id: sensorListView
            x: Constants.DEFAULT_MARGIN
            width: parent.width - Constants.DEFAULT_MARGIN * 2
            height: Constants.DEFAULT_LABEL_HEIGHT * readingValueModel.rowCount
                    + Constants.DEFAULT_SPACING * (readingValueModel.rowCount - 1);
            anchors.top: dataRateSlider.bottom
            spacing: Constants.DEFAULT_SPACING
            interactive: false

            model: readingValueModel

            delegate: TextLabel {
                width: parent ? parent.width : 0;
                height: Constants.DEFAULT_LABEL_HEIGHT
                text: value
                fontPixelSize: Constants.DEFAULT_FONT_PIXEL_SIZE
            }
        }
    }

    ScrollDecorator {
        flickableItem: sensorContentItem
    }

    tools: ToolBarLayout {
        ToolIcon {
            iconId: "toolbar-back";
            onClicked: pageStack.pop();
        }
    }
}

// End of file.
